package com.cs.archivesmanagement.serviceimpl;

import com.cs.archivesmanagement.dao.EntityDao;
import com.cs.archivesmanagement.entity.*;
import com.cs.archivesmanagement.service.ArchivesService;
import org.hibernate.criterion.Criterion;
import org.hibernate.criterion.MatchMode;
import org.hibernate.criterion.Restrictions;
import org.springframework.stereotype.Repository;

import javax.annotation.Resource;
import java.sql.Timestamp;
import java.util.ArrayList;

/**
 * Created by amo on 2015/8/24.
 */
@Repository
public class ArchivesServiceImpl implements ArchivesService {



    @Resource
    EntityDao entityDao;


    @Override
    public boolean addArchives(AmArchives amArchives) {
      return   entityDao.addEntity(amArchives)!=null;
    }

    @Override
    public AmArchives findById(Object id) {
        return (AmArchives)entityDao.getEntity(AmArchives.class,id);
    }

    @Override
    public boolean addArchiveRequest(AmArchivesRequests amArchivesRequests) {
        return entityDao.addEntity(amArchivesRequests)!=null;
    }

    @Override
    public ArrayList<AmArchives> getAllAmArchivesByParameter(Integer departmentId, Integer TypeId, String keyword, Integer accountId, Integer archivesStatus) {
        ArrayList<Criterion> criterions=new ArrayList<>();

        if(departmentId!=null) {
            AmDepartment amDepartment = new AmDepartment();
            amDepartment.setDepartmentId(departmentId);
            criterions.add(Restrictions.eq("archivesDepartmentId", departmentId));
        }

        if(accountId!=null) {
            AmAccount amAccount = new AmAccount();
            amAccount.setAccountId(accountId);
            criterions.add(Restrictions.eq("archivesSummittedUserId", amAccount));
        }

        if(TypeId!=null) {

            AmFileType amFileType=new AmFileType();
            amFileType.setFileTypeId(TypeId);
            criterions.add(Restrictions.eq("archivesTypeId", amFileType));
        }

        if(archivesStatus!=null) {

            criterions.add(Restrictions.eq("archivesStatus", archivesStatus));

        }

        if(keyword!=null){

            criterions.add(Restrictions.like("archivesAbstract", keyword, MatchMode.ANYWHERE));

        }

        return  (ArrayList<AmArchives>)entityDao.getEntityList(AmArchives.class,criterions);
    }

    @Override
    public ArrayList<AmFileType> getAllFileType() {
        return  ( ArrayList<AmFileType> )entityDao.getEntityList(AmFileType.class,null);
    }

    @Override
    public boolean archivesRequest(AmArchives amArchives,Integer operationType,Timestamp startTime,Timestamp endTime,Integer applyerId) {


        //所有对档案的请求，在构造的时候，都默认是pending状态，即等待处理

        AmArchivesRequests amArchivesRequests=new AmArchivesRequests();

        //如果是增加档案的请求，先把要增加的档案上传到服务器，此时archives的状态是默认值invailed，无效，不能被别人看到


        if(operationType==AmArchivesRequests.archivesRequestsType_add) {
            amArchivesRequests.setArchivesRequestsUserId((AmAccount) entityDao.getEntity(AmAccount.class, amArchives.getArchivesSummittedUserId()));
            amArchivesRequests.setArchivesRequestsType(AmArchivesRequests.archivesRequestsType_add);

            Integer archiveId=(Integer)entityDao.addEntity(amArchives);
            amArchives.setArchivesId(archiveId);
            amArchivesRequests.setArchivesRequestsArchiveId(amArchives);

        }



        //如果是要删除档案的请求，则不对要删除的档案作任何处理，单纯增加一条删除请求

        else if(operationType==AmArchivesRequests.archivesRequestsType_delete){

            amArchivesRequests.setArchivesRequestsUserId((AmAccount) entityDao.getEntity(AmAccount.class, applyerId));
            amArchivesRequests.setArchivesRequestsType(AmArchivesRequests.archivesRequestsType_delete);

            amArchivesRequests.setArchivesRequestsArchiveId(amArchives);

        }



        else  if(operationType==amArchivesRequests.archivesRequestsType_modify){

            amArchivesRequests.setArchivesRequestsUserId((AmAccount) entityDao.getEntity(AmAccount.class, applyerId));
            amArchivesRequests.setArchivesRequestsType(AmArchivesRequests.archivesRequestsType_modify);

            //如果要修改，新的档案必须对旧档案进行引用
            amArchives.setArefreceParentId(amArchives.getArchivesId());

            Integer archiveId=(Integer)entityDao.addEntity(amArchives);
            amArchives.setArchivesId(archiveId);
            amArchivesRequests.setArchivesRequestsArchiveId(amArchives);



        }

        else{
            amArchivesRequests.setArchivesRequestsUserId((AmAccount) entityDao.getEntity(AmAccount.class, applyerId));

            amArchivesRequests.setArchivesRequestsType(AmArchivesRequests.archivesRequestsType_modify);
            amArchivesRequests.setArchivesRequestsArchiveTime(startTime);
            amArchivesRequests.setArchivesRequestsArchiveEndTime(endTime);
            amArchivesRequests.setArchivesRequestsType(AmArchivesRequests.archivesRequestsType_get);
            amArchivesRequests.setArchivesRequestsUserId((AmAccount) entityDao.getEntity(AmAccount.class, applyerId));


        }


        return   entityDao.addEntity(amArchivesRequests)!=null;

    }


    @Override
    public String uploadFile(Byte[] bytes) {
        return null;
    }

    @Override
    public int getAmArchivesCount(Integer departmentId, Integer TypeId, String keyword, Integer accountId, Integer archivesStatus) {
        return 0;
    }
}
